<?php
class chapter_tvbcom_category extends Model{
	public static function getTableName(){
		return 'chapter-tvbcom_category';
	}

    public function getPrimaryKey()
    {
        return array('chapter_id', 'category_id');
    }

	public function delBySql($id)
	{
		$sql = "delete from `".self::getTableName()."` where chapter_id='{$id}'";
		return Model::$dbo["w"]->getDbHandle()->exec($sql);
	}

	public function getCategoryByChapterID($id)
	{
        $sql = "SELECT * FROM `".self::getTableName()."` a, tvbcom_category b 
            WHERE a.chapter_id='{$id}' AND a.category_id=b.id
            ORDER BY a.rank";
		return Model::$dbo["r"]->findBySql(__class__, $sql);
	}

	/*
	 * by donald
	 * modified at 2011-09-27
	 */
	public function add_change_log($programme_id, $is_active = 1)
	{
		$field['type'] = $this->getTableName();
		$field['keys'] = array(
			'chapter_id' => $this->chapter_id,
			'category_id' => $this->category_id
		);
		$field['is_active'] = $is_active; 
		$field['programme_id'] = $programme_id;
		$field['episode_id'] = 0; 
		$field['is_publish'] = 0;

		change_log::updateOne($field);
	}

	/*
	 * by donald
	 * modified at 2011-10-11
	 */
	public static function getAll($chapter_id)
	{
		return self::$dbo['w']->find(__CLASS__, 'chapter_id = ?', array($chapter_id));
	}

	/*
	 * by donald
	 */
	public function delete_change_log()
	{
		$type = $this->getTableName();
		$keys = array(
			'chapter_id' => $this->chapter_id,
			'category_id' => $this->category_id
		);
		$is_publish = 0;

		change_log::delTypeAndKeysLog($type, $keys, $is_publish);
	}

}
?>
